package com.sunye.upload.config;

/**
 * @author sunye
 * @date 2022/8/4--15:06
 */
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import lombok.Data;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.github.tobato.fastdfs.FdfsClientConstants;

/**
 * 缩略图配置参数
 *
 * @author tobato
 *
 */
@Component
@Configurable
@Data
public class OwnThumbImageConfig {

    @Value("${fdfs.thumbImage.widthTwo}")
    private int widthTwo;

    @Value("${fdfs.thumbImage.heightTwo}")
    private int heightTwo;

    private static String cachedPrefixName;

    /**
     * 生成前缀如:_150x150
     */
    public String getPrefixName() {
        if (cachedPrefixName == null) {
            StringBuilder buffer = new StringBuilder();
            buffer.append("_").append(widthTwo).append("x").append(heightTwo);
            cachedPrefixName = new String(buffer);
        }
        return cachedPrefixName;
    }

    /**
     * 根据文件名获取缩略图路径
     */
    public String getThumbImagePath(String masterFilename) {
        Validate.notBlank(masterFilename, "主文件不能为空");
        StringBuilder buff = new StringBuilder(masterFilename);
        int index = buff.lastIndexOf(".");
        buff.insert(index, getPrefixName());
        return buff.toString();
    }


}
